home *** CD-ROM | disk | FTP | other *** search
/ Linux Cubed Series 4: GNU Archives / Linux Cubed Series 4 - GNU Archives.iso / gnu / git-4.3 / git-4 / git-4.3.11 / src / gitcmp.c < prev    next >
Encoding:
C/C++ Source or Header  |  1996-04-22  |  3.1 KB  |  137 lines

  1. /* gitcmp.c -- A file compare utility.  */
  2.  
  3. /* Copyright (C) 1993, 1994, 1995 Free Software Foundation, Inc.
  4.  
  5.    This program is free software; you can redistribute it and/or modify
  6.    it under the terms of the GNU General Public License as published by
  7.    the Free Software Foundation; either version 2, or (at your option)
  8.    any later version.
  9.  
  10.    This program is distributed in the hope that it will be useful,
  11.    but WITHOUT ANY WARRANTY; without even the implied warranty of
  12.    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  13.    GNU General Public License for more details.
  14.  
  15.    You should have received a copy of the GNU General Public License
  16.    along with this program; if not, write to the Free Software
  17.    Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.  */
  18.  
  19. /* Written by Tudor Hulubei and Andrei Pitis.  */
  20.  
  21.  
  22. #ifdef HAVE_CONFIG_H
  23. #include <config.h>
  24. #endif
  25.  
  26. #include <stdio.h>
  27.  
  28. #ifdef HAVE_STDLIB_H
  29. #include <stdlib.h>
  30. #else /* !HAVE_STDLIB_H */
  31. #include "ansi_stdlib.h"
  32. #endif /* !HAVE_STDLIB_H */
  33.  
  34. #include <sys/types.h>
  35. #include "file.h"
  36. #include <fcntl.h>
  37. #include <limits.h>
  38.  
  39. #ifdef HAVE_UNISTD_H
  40. #include <unistd.h>
  41. #endif /* HAVE_UNISTD_H */
  42.  
  43.  
  44. #define CMP_BUFFER_SIZE         64*1024
  45. #undef min
  46. #define min(a, b) ((a) <= (b) ? (a) : (b))
  47.  
  48.  
  49. char *program;
  50.  
  51.  
  52. int
  53. file_length(handle)
  54.     int handle;
  55. {
  56.     int tmp, length;
  57.  
  58.     tmp    = lseek(handle, 0, SEEK_CUR);
  59.     length = lseek(handle, 0, SEEK_END);
  60.     lseek(handle, tmp, SEEK_SET);
  61.     return length;
  62. }
  63.  
  64.  
  65. int
  66. main(argc, argv)
  67.     int argc;
  68.     char *argv[];
  69. {
  70.     char *buf1, *buf2;
  71.     int handle1, handle2, i, j;
  72.     size_t len, len1, len2, bytes_to_compare;
  73.  
  74.     program = argv[0];
  75.  
  76.     if (argc != 3)
  77.     {
  78.     fprintf(stderr, "%s: invalid command line.\n", program);
  79.     return 1;
  80.     }
  81.  
  82.     if ((handle1 = open(argv[1], O_RDONLY)) == -1)
  83.     {
  84.     fprintf(stderr, "%s: can't open file '%s'.\n", program, argv[1]);
  85.     return 1;
  86.     }
  87.  
  88.     if ((handle2 = open(argv[2], O_RDONLY)) == -1)
  89.     {
  90.     fprintf(stderr, "%s: can't open file '%s'.\n", program, argv[2]);
  91.     return 1;
  92.     }
  93.  
  94.     if ((len1 = file_length(handle1)) != (len2 = file_length(handle2)))
  95.     fprintf(stderr, "%s: files have different sizes.\n", program);
  96.  
  97.     len = min(len1, len2);
  98.  
  99.     if ((buf1 = (char *)malloc(CMP_BUFFER_SIZE)) == NULL ||
  100.     (buf2 = (char *)malloc(CMP_BUFFER_SIZE)) == NULL)
  101.     {
  102.     fprintf(stderr, "%s: virtual memory exhausted.\n", program);
  103.     return 1;
  104.     }
  105.  
  106.     for (i = 0; i < len; i += CMP_BUFFER_SIZE)
  107.     {
  108.     bytes_to_compare = min(len - i, CMP_BUFFER_SIZE);
  109.  
  110.     if (read(handle1, buf1, bytes_to_compare) != bytes_to_compare)
  111.     {
  112.         fprintf(stderr, "%s: can't read from file %s.\n",
  113.             program, argv[1]);
  114.         return 1;
  115.     }
  116.  
  117.     if (read(handle2, buf2, bytes_to_compare) != bytes_to_compare)
  118.     {
  119.         fprintf(stderr, "%s: can't read from file %s.\n",
  120.             program, argv[2]);
  121.         return 1;
  122.     }
  123.  
  124.     if (memcmp(buf1, buf2, bytes_to_compare))
  125.         for (j = 0; j < bytes_to_compare; j++)
  126.         if (buf1[j] != buf2[j])
  127.         {
  128.             fprintf(stderr, "%s: files differ at offset %d.\n",
  129.                 program, i + j);
  130.             return 1;
  131.         }
  132.     }
  133.  
  134.     fprintf(stderr, "Compare OK\n");
  135.     return 1;
  136. }
  137.